//
//  PayTicketViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/23.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "PayTicketViewController.h"
#import "PayTicketHeaderTableCell.h"
#import "PayTicketDescTableCell.h"
#import "PayTicketPayTypeTableCell.h"
#import "TicketViewController.h"
#import "PaySuccessViewController.h"

@interface PayTicketViewController () <PayTicketDescDelegate,SelectedTickedDelegate>

@property (weak, nonatomic) IBOutlet UITableView *mainTable;
- (IBAction)payAction:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom_top_line;




@end

@implementation PayTicketViewController


-(void)awakeFromNib{
     self.payTicketViewModel = [[PayTicketViewModel alloc ] init];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBtnItem];
    
    self.navigationItem.title = @"支付";
    self.bottom_top_line.constant = 0.45;
    
    [self setExtraCellLineHidden:self.mainTable];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 80;
    }else if (indexPath.section == 1) {
        return 132;
    }else{
        return 44;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        UIView *sectionView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, [UIScreen width], 0.01)];
        return sectionView;
    }
    UIView *sectionView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, [UIScreen width], 40)];
    UILabel *textLabel = [[UILabel alloc ] initWithFrame:CGRectMake(14, 0, [UIScreen width] - 28, 40)];
    textLabel.textColor = [UIColor darkGrayColor];
    textLabel.font = [UIFont systemFontOfSize:13];
    [sectionView addSubview:textLabel];
    
    if (section == 1) {
        textLabel.text = @"购票详情";
    }else{
        textLabel.text = @"支付方式";
    }
    return sectionView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 1;
    }else{
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        PayTicketHeaderTableCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"PayTicketHeaderTableCell"];
        [headerCell setCellPayTicketStyle:self.payTicketStyle];
        return headerCell;
    }else if (indexPath.section == 1){
        PayTicketDescTableCell *descCell = [tableView dequeueReusableCellWithIdentifier:@"PayTicketDescTableCell"];
        descCell.delegate = self;
        descCell.countLabel.text = @(self.payTicketViewModel.ticketCount).stringValue;
        descCell.discountPrice.text = [NSString stringWithFormat:@"-%@元",@(self.payTicketViewModel.discountPrice).stringValue];
        [descCell setCellPayTicketStyle:self.payTicketStyle];
        return descCell;
    }else{
        PayTicketPayTypeTableCell *payTypeCell = [tableView dequeueReusableCellWithIdentifier:@"PayTicketPayTypeTableCell"];
        payTypeCell.indexPath = indexPath;
        [payTypeCell selectedPayType:self.payTicketViewModel.selectedPayType];
        return payTypeCell;
    }
}

- (void)addBtnAction:(UIButton *)sender{
    self.payTicketViewModel.ticketCount ++;
    [self.mainTable reloadData];
}

- (void)mintueBtnAction:(UIButton *)sender{
    self.payTicketViewModel.ticketCount --;
    [self.mainTable reloadData];
}

- (void)selectedSaleTicketBtnAction:(UIButton *)sender{
    TicketViewController *ticket = [[UIStoryboard storyboardWithName:@"MyTicket" bundle:nil]  instantiateViewControllerWithIdentifier:@"TicketViewController"];
    ticket.isSelect = YES;
    ticket.delegate = self;
    [self.navigationController pushViewController:ticket animated:YES];
}

-(void)selectedTicket:(id)model{
    self.payTicketViewModel.discountPrice = [model floatValue];
    [self.mainTable reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        self.payTicketViewModel.selectedPayType = indexPath.row;
        [self.mainTable reloadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)payAction:(UIButton *)sender {
    PaySuccessViewController *paySuccess = [[UIStoryboard storyboardWithName:@"Ticket" bundle:nil]  instantiateViewControllerWithIdentifier:@"PaySuccessViewController"];
    [self.navigationController pushViewController:paySuccess animated:YES];
    
}
@end
